package com.woniu.service.impl;

import com.woniu.dao.CourseDao;
import com.woniu.dao.CourseSectionDao;
import com.woniu.domain.*;
import com.woniu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
/**
 * Date: 2021/6/8:09:26  <br/>
 * Description:课程业务实现类
 * @author 刘家元
 */

@Service
public class CourseServiceImpl implements CourseService {
    @Autowired(required = false)
    CourseDao courseDao;
    @Autowired(required = false)
    CourseSectionService courseSectionService;
    @Autowired(required = false)
    CoursewareService coursewareService;
    @Autowired(required = false)
    CourseElectiveitemService courseElectiveitemService;

    @Autowired
    UsersService usersService;

    @Override
    public List<Course> courseQuery() {
        return courseDao.courseQuery();
    }

    @Override
    public int courseDelById(Integer id) {
        return courseDao.courseDelById(id);
    }

    @Override
    public List<Course> courseConditionQuery(Course obj) {
        return courseDao.courseConditionQuery(obj);
    }

    @Override
    public Course courseGetById(Integer id) {
        return courseDao.courseGetById(id);
    }

    @Override
    public List<Course> findCoursesByIds(String[] cids) {
        return courseDao.findCoursesByIds(cids);
    }

    @Override
    @Transactional
    public int addCourse(CourseAndChapters courseAndChapters) {
        int i=0;
        i+=courseDao.addCourse(courseAndChapters.getCourse());
        Course course = courseDao.getByCourseName(courseAndChapters.getCourse().getCourseName());
        List<CourseSection> courseSections=new ArrayList<>();
        for (int j = 0; j <courseAndChapters.getChapters().size() ; j++) {
            CourseSection courseSection = new CourseSection();
            courseSection.setCourseId(course.getId());
            courseSection.setChapter(courseAndChapters.getChapters().get(j));
            courseSection.setSelectionNumber(j + 1);
            courseSections.add(courseSection);
        }
        i+= courseSectionService.addCourseSection(courseSections);


        courseSections = courseSectionService.getCourseSectionByChapter(courseAndChapters.getChapters());
        ArrayList<Courseware> coursewareList = new ArrayList<>();
        List<String> coursewares = courseAndChapters.getCoursewares();
        for (int j = 0; j < coursewares.size(); j++) {
            String[] split = coursewares.get(j).split(",");
            for (String s : split) {
                if(s != "" && s != null){
                    Courseware courseware = new Courseware();
                    courseware.setChaptertId(courseSections.get(j).getId());
                    courseware.setCoursewareName(s);
                    coursewareList.add(courseware);
                }
            }
        }
        i+=coursewareService.batchUpdateCourseware(coursewareList);
        if(courseAndChapters.getUserNames().size()>0){
            i+= usersService.updateUserCourseNum(courseAndChapters.getUserNames());
            List<User> users = usersService.getUserByName(courseAndChapters.getUserNames());
            List<CourseElectiveitem> courseElectiveitems=new ArrayList<>();
            for (int j = 0; j <users.size() ; j++) {
                CourseElectiveitem courseElectiveitem = new CourseElectiveitem();
                courseElectiveitem.setCourseId(course.getId());
                courseElectiveitem.setUserId(users.get(j).getId());
                courseElectiveitems.add(courseElectiveitem);
            }
            i+= courseElectiveitemService.addCourseElectiveitem(courseElectiveitems);
        }
        return i;
    }

    @Override
    public int updateCourse(Course course) {
        return courseDao.updateCourse(course);
    }

    @Override
    public Course getByCourseName(String courseName) {
        return courseDao.getByCourseName(courseName);
    }

    @Override
    public List<Course> batchQueryCourseId(List<String> courseIds) {
        return courseDao.batchQueryCourseId(courseIds);

    }


}
